package com.czn.service.impl;

import com.czn.dao.HospitalizationDao;
import com.czn.entity.Hospitalization;
import com.czn.service.IHospitalizationService;
import com.czn.vo.QueryVo;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ChenZeNan
 * @create 2021-04-24 19:49
 */
@Service
public class HospitalizationServiceImpl implements IHospitalizationService {

    @Autowired
    private HospitalizationDao hospitalizationDao;

    /**
     * 模糊/分页查询
     *
     * @param queryVo
     * @return
     */
    @Override
    public PageQuery<Hospitalization> findHospitalApplicationList(QueryVo queryVo) {
        PageQuery<Hospitalization> query = new PageQuery<>(queryVo.getPageNo(), queryVo.getPageSize());
        if (!StringUtils.isEmpty(queryVo.getPatientName())) {
            query.setPara("patientName", '%' + queryVo.getPatientName() + '%');
        }
        if (!StringUtils.isEmpty(queryVo.getInTime())) {
            query.setPara("intime", queryVo.getInTime());
        }
        if (queryVo.getUserId() != null) {
            query.setPara("userId", queryVo.getUserId());
        }
        hospitalizationDao.selectLit(query);
        return query;
    }

    /**
     * 根据id查找住院记录
     *
     * @param id
     * @return
     */
    @Override
    public Hospitalization findHospitalizationById(Integer id) {
        Hospitalization hospitalization = hospitalizationDao.selectOne(id);
        return hospitalization;
    }

    /**
     * 添加住院信息
     *
     * @param hospitalization
     * @return
     */
    @Override
    public ResponseResult addHospitalization(Hospitalization hospitalization) {
        ResponseResult result = new ResponseResult();
        int hosId = hospitalizationDao.insertReturnKey(hospitalization).getInt();
        result.setCode("300");
        result.setMessage("信息添加成功！");
        return result;
    }

    /**
     * 编辑住院信息
     *
     * @param hospitalization
     * @return
     */
    @Override
    public ResponseResult editHospitalization(Hospitalization hospitalization) {
        ResponseResult result = new ResponseResult();
        hospitalizationDao.updateTemplateById(hospitalization);
        result.setCode("500");
        result.setMessage("信息更新成功！");
        return result;
    }

    /**
     * 删除住院信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteHospitalizationById(Integer id) {
        ResponseResult result = new ResponseResult();
        int rows = hospitalizationDao.deleteById(id);
        if (rows > 0) {
            result.setCode("400");
            result.setMessage("删除成功！");
            return result;
        }
        result.setCode("401");
        result.setMessage("删除失败！");
        return result;
    }

    /**
     * 查询所有住院记录
     *
     * @return
     */
    @Override
    public List<Hospitalization> findAll() {
        List<Hospitalization> hospitalizations = hospitalizationDao.selectAll();
        return hospitalizations;
    }

    /**
     * 根据患者id查询最新的一条住院记录
     *
     * @param userId
     * @return
     */
    @Override
    public Hospitalization findOneById(Integer userId) {
        Hospitalization hospitalization = hospitalizationDao.selectOneById(userId);
        return hospitalization;
    }

    /**
     * 根据患者id查询所有住院记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<Hospitalization> findListById(Integer userId) {
        return hospitalizationDao.selectListById(userId);
    }
}
